﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TemplateMatch
{
    public partial class dlgMatchConfig : Form
    {
        public dlgMatchConfig(MatchConfig config)
        {
            InitializeComponent();
            this.config = config;
        }

        private MatchConfig config;

        private void dlgMatchConfig_Load(object sender, EventArgs e)
        {
            //pre-treatment
            updownBlurKernelSize.Value = config.BlurKernelSize;
            updownMorphShapeSize.Value = config.MorphShapeSize;
            chkCanny.Checked = config.DoCanny;
            chkTopHat.Checked = config.DoTopHat;

            //match
            updownXScaleFrom.Value = (decimal)config.xScaleFrom;
            updownXScaleTo.Value = (decimal)config.xScaleTo;
            updownXScaleStep.Value = (decimal)config.xStep;
            updownYScaleFrom.Value = (decimal)config.xScaleFrom;
            updownYScaleTo.Value = (decimal)config.xScaleTo;
            updownYScaleStep.Value = (decimal)config.yStep;
            updownThresh.Value = (decimal)config.Thresh;
            chkReform.Checked = config.Reform;
        }

        public MatchConfig GetUpdatedConfig()
        {
            return this.config;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            //this.Close();
        }

        private void UpdateConfig()
        {
            //match
            config.Thresh = (double)updownThresh.Value;
            config.Reform = chkReform.Checked;
            config.xScaleFrom = (double)updownXScaleFrom.Value;
            config.xScaleTo = (double)updownXScaleTo.Value;
            config.xStep = (double)updownXScaleStep.Value;
            config.yScaleFrom = (double)updownYScaleFrom.Value;
            config.yScaleTo = (double)updownYScaleTo.Value;
            config.yStep = (double)updownYScaleStep.Value;

            //pre-treatment
            config.DoCanny = chkCanny.Checked;
            config.DoTopHat = chkTopHat.Checked;
            config.BlurKernelSize = (int)updownBlurKernelSize.Value;
            config.MorphShapeSize = (int)updownMorphShapeSize.Value;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dlgMatchConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (updownXScaleFrom.Value != updownXScaleTo.Value && updownXScaleStep.Value == 0)
            {
                MessageBox.Show("Invalid scale range or step!");
                e.Cancel = true;
                return;
            }
            if (updownYScaleFrom.Value != updownYScaleTo.Value && updownYScaleStep.Value == 0)
            {
                MessageBox.Show("Invalid scale range or step!");
                e.Cancel = true;
                return;
            }

            UpdateConfig();
        }
    }
}
